package com.lst.common;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.lst.dao.MstCodeMapper;
import com.lst.model.ClientAnswerPoint;
import com.lst.model.ClientBody;
import com.lst.model.ClientEvaluate;
import com.lst.model.MstCode;
import com.lst.model.OrdinaryModel;
import com.lst.model.vo.ClientAnswerVo;

public class ViewMyTest {
    private static ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
	    "applicationContext.xml");
    @Autowired
    private static MstCodeMapper mstCodeMapper = ctx
	    .getBean(MstCodeMapper.class);

    public static ClientAnswerVo deal(ClientEvaluate ue) {

	ClientAnswerVo cav = new ClientAnswerVo();
	ClientAnswerPoint cap = new ClientAnswerPoint();
	cap.setClientid(ue.getClientid());

	// 根据身高和体重计算出BMI匹配指数(体重（kg）/(身高（m）的平方))
	BigDecimal weight = ue.getWeight();
	BigDecimal height = ue.getHeight();

	BigDecimal BMI = weight.divide(height.multiply(height), 2,
		BigDecimal.ROUND_HALF_UP);

	if (BMI.compareTo(new BigDecimal("18.4")) < 1) {
	    // 当BMI值<=18.4时
	    cap.setZhgd(new BigDecimal(1));
	    cap.setSd(new BigDecimal(1));
	    cap.setTq(new BigDecimal(1));
	    cap.setTqd(new BigDecimal(1));
	    cap.setKsd(new BigDecimal(1));
	    cap.setQj(new BigDecimal(1));
	    cap.setWstl(new BigDecimal(1));
	    cap.setBxrs(new BigDecimal(1));
	    cav.setBodyStatus(-1);
	} else if (BMI.compareTo(new BigDecimal("28")) >= 0) {
	    // 当BMI值>=28时
	    cap.setTqd(new BigDecimal(1));
	    cap.setKsd(new BigDecimal(1));
	    cap.setJd(new BigDecimal(1));
	    cap.setWstl(new BigDecimal(1));
	    cap.setYc(new BigDecimal(1));
	    cap.setMjs(new BigDecimal(1));
	    cap.setHqd(new BigDecimal(1));
	    cap.setTjys(new BigDecimal(1));
	    cap.setBxzw(new BigDecimal(1));
	    cav.setBodyStatus(1);
	} else {
	    cap.setZhgd(new BigDecimal(1));
	    cap.setSd(new BigDecimal(1));
	    cap.setTq(new BigDecimal(1));
	    cap.setTqd(new BigDecimal(1));
	    cap.setKsd(new BigDecimal(1));
	    cap.setQj(new BigDecimal(1));
	    cap.setJd(new BigDecimal(1));
	    cap.setWstl(new BigDecimal(1));
	    cap.setYc(new BigDecimal(1));
	    cap.setMjs(new BigDecimal(1));
	    cap.setBxrs(new BigDecimal(1));
	    cap.setHqd(new BigDecimal(1));
	    cap.setTjys(new BigDecimal(1));
	    cap.setBxzw(new BigDecimal(1));
	    cav.setBodyStatus(0);
	}
	// 根据年龄计算
	int age = ue.getAge();
	if (age <= 10) {
	    cap.setTqd(cap.getTqd().add(new BigDecimal(1)));
	    cap.setKsd(cap.getKsd().add(new BigDecimal(1)));
	    cap.setWstl(cap.getWstl().add(new BigDecimal(1)));
	    cap.setYc(cap.getYc().add(new BigDecimal(1)));

	} else if (age >= 50) {
	    cap.setJd(cap.getJd().add(new BigDecimal(1)));
	    cap.setHqd(cap.getHqd().add(new BigDecimal(1)));
	    cap.setTjys(cap.getTjys().add(new BigDecimal(1)));
	} else {

	}
	// 计算身体机能匹配指数
	cav = body_function(ue, cap, cav);

	return cav;
    }

    /**
     * 
     * @Title: body_function
     * @Description: 身体机能匹配指数
     * @param 设定文件
     * @return void 返回类型
     * @throws
     */
    public static ClientAnswerVo body_function(ClientEvaluate ue,
	    ClientAnswerPoint cap, ClientAnswerVo cav) {

	// 身体机能匹配指数
	// 深蹲
	int squatting = ue.getSquatting();
	BigDecimal _squatting = new BigDecimal(squatting);
	// 前后分腿蹲
	int re_Squatting = ue.getReSquatting();
	BigDecimal _re_Squatting = new BigDecimal(re_Squatting);
	// 肩关节
	int shoulder_joint = ue.getShoulderJoint();
	BigDecimal _shoulder_joint = new BigDecimal(shoulder_joint);

	// 直腿上抬
	int straight_leg_lift = ue.getStraightLegLift();
	BigDecimal _straight_leg_lift = new BigDecimal(straight_leg_lift);

	// 平底撑
	int flat_bottom = ue.getFlatBottom();
	BigDecimal _flat_bottom = new BigDecimal(flat_bottom);

	// 高抬腿
	int high_leg_lift = ue.getHighLegLift();
	BigDecimal _high_leg_lift = new BigDecimal(high_leg_lift);

	// 仰卧举腿
	int supine_leg_lift = ue.getSupineLegLift();
	BigDecimal _supine_leg_lift = new BigDecimal(supine_leg_lift);

	// 横叉
	int cross_fork = ue.getCrossFork();
	BigDecimal _cross_fork = new BigDecimal(cross_fork);
	// 身体机能实体
	ClientBody cb = new ClientBody();
	cb.setClientid(ue.getClientid());
	// 排序的身体机能list
	List<OrdinaryModel> bodyList = new ArrayList<OrdinaryModel>();
	// 速度
	BigDecimal speed = _high_leg_lift.multiply(new BigDecimal("0.7"))
		.add(_supine_leg_lift.multiply(new BigDecimal("0.3")))
		.setScale(2, BigDecimal.ROUND_HALF_UP);
	cb.setSpeed(speed);
	OrdinaryModel od1 = new OrdinaryModel(1, "速度", speed,
		CommNote.NOTE_SPEED,CommNote.ICON_REPORT_1);
	bodyList.add(od1);
	// 力量(上)
	BigDecimal power_up = _flat_bottom;
	cb.setPowerUp(power_up);
	OrdinaryModel od2 = new OrdinaryModel(2, "力量(上)", power_up,
		CommNote.NOTE_POWER_UP,CommNote.ICON_REPORT_2);
	bodyList.add(od2);
	// 力量(下)
	BigDecimal power_down = _squatting.multiply(new BigDecimal("0.4"))
		.add(_re_Squatting.multiply(new BigDecimal("0.1")))
		.add(_high_leg_lift.multiply(new BigDecimal("0.5")))
		.setScale(2, BigDecimal.ROUND_HALF_UP);
	cb.setPowerDown(power_down);
	OrdinaryModel od3 = new OrdinaryModel(3, "力量(下)", power_down,
		CommNote.NOTE_POWER_DOWN,CommNote.ICON_REPORT_4);
	bodyList.add(od3);
	// 力量(腰腹)
	BigDecimal power_middle = _straight_leg_lift
		.multiply(new BigDecimal("0.1"))
		.add(_supine_leg_lift.multiply(new BigDecimal("0.5")))
		.add(_flat_bottom.multiply(new BigDecimal("0.4")))
		.setScale(2, BigDecimal.ROUND_HALF_UP);
	cb.setPowerMiddle(power_middle);
	OrdinaryModel od4 = new OrdinaryModel(4, "力量(腰腹)", power_middle,
		CommNote.NOTE_POWER_MIDDLE,CommNote.ICON_REPORT_8);
	bodyList.add(od4);
	// 灵活性
	BigDecimal ligament = _squatting.multiply(new BigDecimal("0.1"))
		.add(_re_Squatting.multiply(new BigDecimal("0.4")))
		.add(_shoulder_joint.multiply(new BigDecimal("0.2")))
		.add(_high_leg_lift.multiply(new BigDecimal("0.3")))
		.setScale(2, BigDecimal.ROUND_HALF_UP);
	cb.setLigament(ligament);
	OrdinaryModel od5 = new OrdinaryModel(5, "灵活性", ligament,
		CommNote.NOTE_LIGAMENT,CommNote.ICON_REPORT_3);
	bodyList.add(od5);
	// 稳定性
	BigDecimal clarity = _squatting.multiply(new BigDecimal("0.25"))
		.add(_re_Squatting.multiply(new BigDecimal("0.3")))
		.add(_straight_leg_lift.multiply(new BigDecimal("0.1")))
		.add(_flat_bottom.multiply(new BigDecimal("0.2")))
		.add(_high_leg_lift.multiply(new BigDecimal("0.15")))
		.setScale(2, BigDecimal.ROUND_HALF_UP);
	cb.setClarity(clarity);
	OrdinaryModel od6 = new OrdinaryModel(6, "稳定性", clarity,
		CommNote.NOTE_CLARITY,CommNote.ICON_REPORT_5);
	bodyList.add(od6);
	// 耐力
	BigDecimal proof = _high_leg_lift.multiply(new BigDecimal("0.5"))
		.add(_supine_leg_lift.multiply(new BigDecimal("0.5")))
		.setScale(2, BigDecimal.ROUND_HALF_UP);
	cb.setProof(proof);
	OrdinaryModel od7 = new OrdinaryModel(7, "耐力", proof,
		CommNote.NOTE_PROOF,CommNote.ICON_REPORT_9);
	bodyList.add(od7);
	// 柔韧性(上)
	BigDecimal softness_up = _shoulder_joint;
	cb.setSoftnessUp(softness_up);
	OrdinaryModel od8 = new OrdinaryModel(8, "柔韧性(上)", softness_up,
		CommNote.NOTE_SOFTNESS_UP,CommNote.ICON_REPORT_10);
	bodyList.add(od8);
	// 柔韧性(下)
	BigDecimal softness_down = _re_Squatting
		.multiply(new BigDecimal("0.1"))
		.add(_straight_leg_lift.multiply(new BigDecimal("0.15")))
		.add(_cross_fork.multiply(new BigDecimal("0.75")))
		.setScale(2, BigDecimal.ROUND_HALF_UP);
	cb.setSoftnessDown(softness_down);
	OrdinaryModel od9 = new OrdinaryModel(9, "柔韧性(下)", softness_down,
		CommNote.NOTE_SOFTNESS_DOWN,CommNote.ICON_REPORT_11);
	bodyList.add(od9);
	// 动作控制力
	BigDecimal restrain = _squatting.multiply(new BigDecimal("0.25"))
		.add(_re_Squatting.multiply(new BigDecimal("0.35")))
		.add(_supine_leg_lift.multiply(new BigDecimal("0.4")))
		.setScale(2, BigDecimal.ROUND_HALF_UP);
	cb.setRestrain(restrain);
	OrdinaryModel od10 = new OrdinaryModel(10, "动作控制力", restrain,
		CommNote.NOTE_RESTRAIN,CommNote.ICON_REPORT_12);
	bodyList.add(od10);
	// 爆发力
	BigDecimal explosive = _high_leg_lift;
	cb.setExplosive(explosive);
	OrdinaryModel od11 = new OrdinaryModel(11, "爆发力", explosive,
		CommNote.NOTE_EXPLOSIVE,CommNote.ICON_REPORT_6);
	bodyList.add(od11);
	// 排序(从小到大)
	Collections.sort(bodyList);
	// 个人身体机能指标分值总和
	BigDecimal body_total = speed.add(power_up).add(power_down)
		.add(power_middle).add(ligament).add(clarity).add(proof)
		.add(softness_up).add(softness_down).add(restrain)
		.add(explosive);
	cb.setTotal(body_total);

	// 感兴趣
	String interest = ue.getInterested();
	// 不感兴趣
	String uninterest = ue.getUninterested();
	// 根据身体情况去匹配相应的项目
	cap = dealBody_match(body_total, cap, interest, uninterest);

	// 如果学习目的为1，并且身体指标小于20(马伽术+1)
	if (ue.getStudyPurpose() == 1
		&& body_total.compareTo(new BigDecimal(20)) < 1) {
	    cap.setMjs(cap.getMjs().add(new BigDecimal(1)));
	}
	List<OrdinaryModel> kungfuList = new ArrayList<OrdinaryModel>();

	OrdinaryModel om1 = new OrdinaryModel(1, "综合格斗(MMA)", cap.getZhgd(),
		CommNote.NOTE_ZHGD, cap.getZhgdDiff(),CommNote.ICON_REPORT_8);
	kungfuList.add(om1);	
	OrdinaryModel om2 = new OrdinaryModel(2, "散打", cap.getSd(),
		CommNote.NOTE_SD, cap.getSdDiff(),CommNote.ICON_REPORT_8);
	kungfuList.add(om2);
	OrdinaryModel om3 = new OrdinaryModel(3, "泰拳", cap.getTq(),
		CommNote.NOTE_TQ, cap.getTqDiff(),CommNote.ICON_REPORT_2);
	kungfuList.add(om3);
	OrdinaryModel om4 = new OrdinaryModel(4, "跆拳道", cap.getTqd(),
		CommNote.NOTE_TQD, cap.getTqdDiff(),CommNote.ICON_REPORT_4);
	kungfuList.add(om4);
	OrdinaryModel om5 = new OrdinaryModel(5, "空手道", cap.getKsd(),
		CommNote.NOTE_KSD, cap.getKsdDiff(),CommNote.ICON_REPORT_2);
	kungfuList.add(om5);
	OrdinaryModel om6 = new OrdinaryModel(6, "拳击", cap.getQj(),
		CommNote.NOTE_QJ, cap.getQjDiff(),CommNote.ICON_REPORT_2);
	kungfuList.add(om6);
	OrdinaryModel om7 = new OrdinaryModel(7, "剑道", cap.getJd(),
		CommNote.NOTE_JD, cap.getJdDiff(),CommNote.ICON_REPORT_11);
	kungfuList.add(om7);
	OrdinaryModel om8 = new OrdinaryModel(8, "武术套路", cap.getWstl(),
		CommNote.NOTE_WSTL, cap.getWstlDiff(),CommNote.ICON_REPORT_11);
	kungfuList.add(om8);
	OrdinaryModel om9 = new OrdinaryModel(9, "咏春", cap.getYc(),
		CommNote.NOTE_YC, cap.getYcDiff(),CommNote.ICON_REPORT_2);
	kungfuList.add(om9);
	OrdinaryModel om10 = new OrdinaryModel(10, "马伽术", cap.getMjs(),
		CommNote.NOTE_MJS, cap.getMjsDiff(),CommNote.ICON_REPORT_11);
	kungfuList.add(om10);
	OrdinaryModel om11 = new OrdinaryModel(11, "巴西柔术", cap.getBxrs(),
		CommNote.NOTE_BXRS, cap.getBxrsDiff(),CommNote.ICON_REPORT_11);
	kungfuList.add(om11);
	OrdinaryModel om12 = new OrdinaryModel(12, "合气道", cap.getHqd(),
		CommNote.NOTE_HQD, cap.getHqdDiff(),CommNote.ICON_REPORT_11);
	kungfuList.add(om12);
	OrdinaryModel om13 = new OrdinaryModel(13, "太极养生", cap.getTjys(),
		CommNote.NOTE_TJYS, cap.getTjysDiff(),CommNote.ICON_REPORT_11);
	kungfuList.add(om13);
	OrdinaryModel om14 = new OrdinaryModel(14, "巴西战舞", cap.getBxzw(),
		CommNote.NOTE_BXZW, cap.getBxzwDiff(),CommNote.ICON_REPORT_11);
	kungfuList.add(om14);
	Collections.sort(kungfuList);

	cav.setBodylist(bodyList);
	cav.setKungfulist(kungfuList);
	cav.setCap(cap);
	return cav;

    }

    public static ClientAnswerPoint dealBody_match(BigDecimal body_total,
	    ClientAnswerPoint cap, String interest, String uninterest) {
	Map<String, Object> map = new HashMap<String, Object>();
	map.put("parentId", 326);
	List<MstCode> list = mstCodeMapper.queryByList(map, new PageBounds());
	String[] interestArray = interest.split(",");
	String[] uninterestArray = uninterest.split(",");
	for (MstCode mstCode : list) {
	    String code = mstCode.getCode();
	    Integer id = mstCode.getId();
	    Boolean isexist1 = false;
	    // 先默认全部+1(题目感兴趣和不感兴趣)
	    cap = add(id, cap, new BigDecimal(1));
	    for (String in : interestArray) {
		if (in.equals(id.toString())) {
		    isexist1 = true;
		    break;
		}
	    }
	    if (isexist1 == true) {
		// 感兴趣的+1
		cap = add(id, cap, new BigDecimal(1));
	    }
	    Boolean isexist2 = false;
	    for (String in : uninterestArray) {
		if (in.equals(id.toString())) {
		    isexist2 = true;
		    break;
		}
	    }
	    if (isexist2 == true) {
		// 不感兴趣的减1
		cap = add(id, cap, new BigDecimal(-1));
	    }

	    // 身体机能匹配指数
	    BigDecimal diff = new BigDecimal(code).subtract(body_total);
	    if (diff.compareTo(new BigDecimal(-3)) == -1
		    || diff.compareTo(new BigDecimal(6)) == 1) {
		// 小于-3，大于6时不增加
		cap = addDiff(id, cap, new BigDecimal(0), diff);
	    } else if (diff.compareTo(BigDecimal.ZERO) >= 0
		    && diff.compareTo(new BigDecimal(3)) <= 0) {
		// 大于0小于3时，加2
		cap = addDiff(id, cap, new BigDecimal(2), diff);
	    } else {
		// 其他的(大于(-3)小于0，，，大于3小于6)加1
		cap = addDiff(id, cap, new BigDecimal(1), diff);
	    }
	}

	return cap;
    }

    /**
     * 
     * @Title: add
     * @Description: 处理每项的加减
     * @param @param id
     * @param @param cap
     * @param @param val
     * @param @return 设定文件
     * @return ClientAnswerPoint 返回类型
     * @throws
     */
    public static ClientAnswerPoint add(Integer id, ClientAnswerPoint cap,
	    BigDecimal val) {
	// 其他的(大于(-3)小于0，，，大于3小于6)加1
	if (id.equals(CommCode.SYSTEM_KUNGFU_TYPE_ZHGD)) {// 综合格斗
	    cap.setZhgd(cap.getZhgd().add(val));

	} else if (id.equals(CommCode.SYSTEM_KUNGFU_TYPE_SD)) {// 散打
	    cap.setSd(cap.getSd().add(val));
	} else if (id.equals(CommCode.SYSTEM_KUNGFU_TYPE_TQ)) {// 泰拳
	    cap.setTq(cap.getTq().add(val));
	} else if (id.equals(CommCode.SYSTEM_KUNGFU_TYPE_TQD)) {// 跆拳道
	    cap.setTqd(cap.getTqd().add(val));
	} else if (id.equals(CommCode.SYSTEM_KUNGFU_TYPE_KSD)) {// 空手道
	    cap.setKsd(cap.getKsd().add(val));
	} else if (id.equals(CommCode.SYSTEM_KUNGFU_TYPE_QJ)) {// 拳击
	    cap.setQj(cap.getQj().add(val));
	} else if (id.equals(CommCode.SYSTEM_KUNGFU_TYPE_JD)) {// 剑道
	    cap.setJd(cap.getJd().add(val));
	} else if (id.equals(CommCode.SYSTEM_KUNGFU_TYPE_WSTL)) {// 武术套路
	    cap.setWstl(cap.getWstl().add(val));
	} else if (id.equals(CommCode.SYSTEM_KUNGFU_TYPE_YC)) {// 咏春
	    cap.setYc(cap.getYc().add(val));
	} else if (id.equals(CommCode.SYSTEM_KUNGFU_TYPE_MJS)) {// 马伽术
	    cap.setMjs(cap.getMjs().add(val));
	} else if (id.equals(CommCode.SYSTEM_KUNGFU_TYPE_BXRS)) {// 巴西柔术
	    cap.setBxrs(cap.getBxrs().add(val));
	} else if (id.equals(CommCode.SYSTEM_KUNGFU_TYPE_HQD)) {// 合气道
	    cap.setHqd(cap.getHqd().add(val));
	} else if (id.equals(CommCode.SYSTEM_KUNGFU_TYPE_TJYS)) {// 太极养生
	    cap.setTjys(cap.getTjys().add(val));
	} else if (id.equals(CommCode.SYSTEM_KUNGFU_TYPE_BXZW)) {// 巴西战舞
	    cap.setBxzw(cap.getBxzw().add(val));
	}

	return cap;
    }

    public static ClientAnswerPoint addDiff(Integer id, ClientAnswerPoint cap,
	    BigDecimal val, BigDecimal diff) {
	// 其他的(大于(-3)小于0，，，大于3小于6)加1
	if (id.equals(CommCode.SYSTEM_KUNGFU_TYPE_ZHGD)) {// 综合格斗
	    cap.setZhgd(cap.getZhgd().add(val));
	    cap.setZhgdDiff(diff);
	} else if (id.equals(CommCode.SYSTEM_KUNGFU_TYPE_SD)) {// 散打
	    cap.setSd(cap.getSd().add(val));
	    cap.setSdDiff(diff);
	} else if (id.equals(CommCode.SYSTEM_KUNGFU_TYPE_TQ)) {// 泰拳
	    cap.setTq(cap.getTq().add(val));
	    cap.setTqDiff(diff);
	} else if (id.equals(CommCode.SYSTEM_KUNGFU_TYPE_TQD)) {// 跆拳道
	    cap.setTqd(cap.getTqd().add(val));
	    cap.setTqdDiff(diff);
	} else if (id.equals(CommCode.SYSTEM_KUNGFU_TYPE_KSD)) {// 空手道
	    cap.setKsd(cap.getKsd().add(val));
	    cap.setKsdDiff(diff);
	} else if (id.equals(CommCode.SYSTEM_KUNGFU_TYPE_QJ)) {// 拳击
	    cap.setQj(cap.getQj().add(val));
	    cap.setQjDiff(diff);
	} else if (id.equals(CommCode.SYSTEM_KUNGFU_TYPE_JD)) {// 剑道
	    cap.setJd(cap.getJd().add(val));
	    cap.setJdDiff(diff);
	} else if (id.equals(CommCode.SYSTEM_KUNGFU_TYPE_WSTL)) {// 武术套路
	    cap.setWstl(cap.getWstl().add(val));
	    cap.setWstlDiff(diff);
	} else if (id.equals(CommCode.SYSTEM_KUNGFU_TYPE_YC)) {// 咏春
	    cap.setYc(cap.getYc().add(val));
	    cap.setYcDiff(diff);
	} else if (id.equals(CommCode.SYSTEM_KUNGFU_TYPE_MJS)) {// 马伽术
	    cap.setMjs(cap.getMjs().add(val));
	    cap.setMjsDiff(diff);
	} else if (id.equals(CommCode.SYSTEM_KUNGFU_TYPE_BXRS)) {// 巴西柔术
	    cap.setBxrs(cap.getBxrs().add(val));
	    cap.setBxrsDiff(diff);
	} else if (id.equals(CommCode.SYSTEM_KUNGFU_TYPE_HQD)) {// 合气道
	    cap.setHqd(cap.getHqd().add(val));
	    cap.setHqdDiff(diff);
	} else if (id.equals(CommCode.SYSTEM_KUNGFU_TYPE_TJYS)) {// 太极养生
	    cap.setTjys(cap.getTjys().add(val));
	    cap.setTjysDiff(diff);
	} else if (id.equals(CommCode.SYSTEM_KUNGFU_TYPE_BXZW)) {// 巴西战舞
	    cap.setBxzw(cap.getBxzw().add(val));
	    cap.setBxzwDiff(diff);
	}

	return cap;
    }
}
